﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Web;

namespace YUCodeGen.Utils
{
    public class TSUtil
    {
        public static string GetPartial(PropertyInfo pinfo)
        {
            string result = "_inputText";
            if (pinfo.PropertyType == typeof(Boolean))
            {
                result = "_inputBoolean";
            }
            
            return result;
        }
        public static string GetInputType(PropertyInfo pinfo)
        {
            string result = "text";
            if (pinfo.PropertyType == typeof(Boolean))
            {
                result = "checkbox";
            }
            else if (pinfo.PropertyType == typeof(DateTime))
            {
                result = "date";
            }
            else if (IsTel(pinfo))
            {
                result = "tel";
            }
            else if ( IsEmail(pinfo))
            {
                result = "email";
            }
            else if (IsUrl(pinfo))
            {
                result = "url";
            }
            
            return result;
        }

        public static bool IsUrl(PropertyInfo pinfo)
        {
            return pinfo.CustomAttributes.Any(x => x.AttributeType == typeof(UrlAttribute));
        }

        public static bool IsTel(PropertyInfo pinfo)
        {
            return pinfo.CustomAttributes.Any(x => x.AttributeType == typeof(PhoneAttribute));
        }

        public static bool IsEmail(PropertyInfo pinfo)
        {
            return pinfo.CustomAttributes.Any(x => x.AttributeType == typeof(EmailAddressAttribute));
        }

        public static string GetValidatorMaxLength(PropertyInfo pinfo)
        {
            string result = "";
            var maxlengthAttri= pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(MaxLengthAttribute));
            if (maxlengthAttri != null)
            {
                result = $"Validators.maxLength({maxlengthAttri.ConstructorArguments[0].Value}),";
            }
            return result;
        }

        public static string GetValidatorMinLength(PropertyInfo pinfo)
        {
            string result = "";
            var maxlengthAttri = pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(MinLengthAttribute));
            if (maxlengthAttri != null)
            {
                result = $"Validators.minLength({maxlengthAttri.ConstructorArguments[0].Value}),";
            }
            return result;
        }

        public static string GetValidatorEmail(PropertyInfo pinfo)
        {
            string result = "";
            if (IsEmail(pinfo))
            {
                result = "Validators.email,";
            }
            return result;
        }



        public static string GetMinLengthValue(PropertyInfo pinfo)
        {
            string result = "";
            var attri = pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(MaxLengthAttribute));
            if (attri != null)
            {
                result = attri.ConstructorArguments[0].Value.ToString();
            }
            return result;
        }

        

        public static string GetValidatorRequired(PropertyInfo pinfo)
        {
            string result = "";
            var attri = pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(RequiredAttribute));
            if (attri != null)
            {
                result = "Validators.required,";
            }
            return result;
        }

        public static string GetValidatorRegularExp(PropertyInfo pinfo)
        {
            string result = "";
            var attri = pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(RegularExpressionAttribute));
            if (attri!=null)
            {
                result =$"Validators.pattern(/{attri.ConstructorArguments[0].Value}/),";
            }
            return result;
        }

        public static bool IsMultipleText(PropertyInfo pinfo)
        {
            bool result = false;
            var attri = pinfo.CustomAttributes.Where(x => x.AttributeType == typeof(DataTypeAttribute));
            if (attri.Count()>0)
            {
                result = (attri.FirstOrDefault(x =>(int)(x.ConstructorArguments[0].Value) ==(int)DataType.MultilineText) != null);
            }
            return result;
        }

        
    }
}